package com.sporadic.desk.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sporadic.desk.dao.UserInfoDao;
import com.sporadic.desk.pojo.UserInfo;
import com.sporadic.desk.service.UserInfoService;
import dto.UserInfoDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.JwtSecret;
import utils.MD5;

/**
 * @program: sporadic
 * @description:
 * @author: LHD   QQ:1727865856
 * @create: 2019-10-21 15:56
 **/
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoDao, UserInfo> implements UserInfoService {
    @Autowired
    private UserInfoDao userInfoDao;

    @Override
    public UserInfoDTO queryUserInfo(String userName, String pwd) {
        String password = MD5.md5(pwd, MD5.KEY);
        UserInfo userInfo = userInfoDao.queryUserInfo(userName, password);
        UserInfoDTO userInfoDTO=new UserInfoDTO();
        BeanUtils.copyProperties(userInfo,userInfoDTO);
        return userInfoDTO;
    }

    @Override
    public String queryUser(String userName, String pwd) {
        String password = MD5.md5(pwd, MD5.KEY);
        UserInfo userInfo = userInfoDao.queryUserInfo(userName, password);
        UserInfoDTO userInfoDTO=new UserInfoDTO();
        BeanUtils.copyProperties(userInfo,userInfoDTO);
        String jwt = JwtSecret.jwt(userInfoDTO);
        if (jwt==""){
            throw new RuntimeException("登录失败,用户不存在！");
        }
        return jwt;
    }

}
